package com.example.demo;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-14
 * Time: 22:34
 */
@RestController
@RequestMapping("/art")
public class ArticleController {
    /**
     * 向redis中存储数据 + 查询是否含有数据(获取)
     */
    @RequestMapping("/set")
    @Cacheable(value = "spring:cache", key = "#username+'-'+#password")  // 存储的是key值
    public String setRedis(String username, String password) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return "请先输入 username || password";
        }
        return "username: " + username;
    }

    /**
     * 向 redis 中更新缓存
     */
    @RequestMapping("/put")
    @CachePut(value = "spring:cache", key = "#username+'-'+#password")
    public String getRedis(String username, String password) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return "请先输入 username || password";
        }
        return password;
    }

    /**
     * 向redis中删除缓冲
     */

    @RequestMapping("del")
    @Cacheable(value = "spring:cache", key = "#username")
    public String delRedis(String username) {
        return "delete redis success!";
    }
}
